import { Popconfirm } from 'antd';
import { getCurrentUser } from '@/utils/authority';

const mainAccountInfo = getCurrentUser() || {};

const taskStateMap = {
  0: '待执行',
  1: '执行中',
  2: '已完成',
  3: '执行失败',
};

const columnConfig = {
  sort: {
    name: '序号',
    code: 'sort',
    width: 60,
    render(v, r, i) {
      return i + 1;
    }
  },
  taskTypeName: {
    name: "任务类型",
    code: "taskTypeName",
    width: 100
  },
  name: {
    name: "文件名称",
    code: "fileName",
    width: 220,
  },
  state: {
    name: "任务状态",
    code: "state",
    width: 100,
    render: (text) => (
      <span className={text == 3 ? 'common-color9' : ''}>
        {taskStateMap[text]}
      </span>
    ),
  },
  errorMsg: {
    name: "失败原因",
    code: "errorMsg",
    width: 200,
  },
  createTime: {
    name: "创建时间",
    code: "createTime",
    width: 150,
  },
  operAccount: {
    name: "发起人",
    code: "operAccount",
    width: 150,
    render: (text) => {
      if (text && typeof text == 'string') {
        if (text.split('(')[1].split(')')[0] == mainAccountInfo.account) {
          return mainAccountInfo.account;
        } else {
          return text;
        }
      }
    },
  },
  downloadNum: {
    name: "文件下载次数",
    code: "downloadNum",
    width: 120,
  },
  opera: {
    name: "操作",
    code: "roleNames",
    width: 80,
    lock: true
  },
}

//渲染操作列
const RenderOpera = ({ handleOpera, record }) => {
  if (record.state === 0 || record.state === 1) {
    return (
      <Popconfirm
        title="确定要删除吗?"
        onConfirm={() => handleOpera('delete', record.id)}
        okText="确定"
        cancelText="取消"
      >
        <span className="common-color2">删除</span>
      </Popconfirm>
    );
  } else if (record.state === 2) {
    return (
      <span
        className="common-color2"
        onClick={() => handleOpera('export', record.id)}
      >
        下载文件
      </span>
    );
  } else {
    return '无';
  }
}

//获取列数据
export const getColumns = ({ handleOpera }) => {
  const arr = [];
  Object.keys(columnConfig).map(v => {
    const item = columnConfig[v];
    if (v == 'opera') {
      item.render = (t, r) => <RenderOpera handleOpera={handleOpera} record={r} />;
    }
    arr.push(item);
  })
  return arr;
}